package com.aqielife.hk.config;

import com.aqielife.hk.common.OsSelect;
import com.aqielife.hk.sdk.FMSGCallBack_V31;
import com.aqielife.hk.sdk.HCNetSDK;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author aqie
 * @Date 2022/4/19 14:14
 * @desc
 */
@Configuration
public class HaikangConfig {

    private static FMSGCallBack_V31 fMSFCallBack_V31 = null;

    @Bean
    public HCNetSDK hCNetSDK() {
        HCNetSDK hCNetSDK = CreateSDKInstance();
        //linux系统建议调用以下接口加载组件库
        if (OsSelect.isLinux()) {
            HCNetSDK.BYTE_ARRAY ptrByteArray1 = new HCNetSDK.BYTE_ARRAY(256);
            HCNetSDK.BYTE_ARRAY ptrByteArray2 = new HCNetSDK.BYTE_ARRAY(256);
            //这里是库的绝对路径，请根据实际情况修改，注意改路径必须有访问权限
            String strPath1 = System.getProperty("user.dir") + "/lib/libcrypto.so.1.1";
            String strPath2 = System.getProperty("user.dir") + "/lib/libssl.so.1.1";

            System.arraycopy(strPath1.getBytes(), 0, ptrByteArray1.byValue, 0, strPath1.length());
            ptrByteArray1.write();
            hCNetSDK.NET_DVR_SetSDKInitCfg(3, ptrByteArray1.getPointer());

            System.arraycopy(strPath2.getBytes(), 0, ptrByteArray2.byValue, 0, strPath2.length());
            ptrByteArray2.write();
            hCNetSDK.NET_DVR_SetSDKInitCfg(4, ptrByteArray2.getPointer());

            String strPathCom = System.getProperty("user.dir") + "/lib";
            HCNetSDK.NET_DVR_LOCAL_SDK_PATH struComPath = new HCNetSDK.NET_DVR_LOCAL_SDK_PATH();
            System.arraycopy(strPathCom.getBytes(), 0, struComPath.sPath, 0, strPathCom.length());
            struComPath.write();
            hCNetSDK.NET_DVR_SetSDKInitCfg(2, struComPath.getPointer());
        }

        hCNetSDK.NET_DVR_Init();
        boolean i = hCNetSDK.NET_DVR_SetLogToFile(3, "..//sdklog", false);
        //设置报警回调函数
        if (fMSFCallBack_V31 == null) {
            fMSFCallBack_V31 = new FMSGCallBack_V31();
            Pointer pUser = null;
            if (!hCNetSDK.NET_DVR_SetDVRMessageCallBack_V31(fMSFCallBack_V31, pUser)) {
                System.out.println("设置回调函数失败!");
            } else {
                System.out.println("设置回调函数成功!");
            }
        }
        return hCNetSDK;
    }

    /**
     * 根据不同操作系统选择不同的库文件和库路径
     *
     * @return
     */
    private static HCNetSDK CreateSDKInstance() {
        synchronized (HCNetSDK.class) {
            String strDllPath = "";
            try {
                //System.setProperty("jna.debug_load", "true");
                if (OsSelect.isWindows())
                    //win系统加载库路径
                    strDllPath = System.getProperty("user.dir") + "\\lib\\HCNetSDK.dll";
                else if (OsSelect.isLinux())
                    //Linux系统加载库路径
                    strDllPath = System.getProperty("user.dir") + "/lib/libhcnetsdk.so";
                HCNetSDK hCNetSDK = (HCNetSDK) Native.loadLibrary(strDllPath, HCNetSDK.class);
                return hCNetSDK;
            } catch (Exception ex) {
                System.out.println("loadLibrary: " + strDllPath + " Error: " + ex.getMessage());
                return null;
            }
        }

    }
}
